/*
商店管理
*/
window._MSHOP = {

    _CURRENTLABEL:null,

    _CURRENTNPCTYPE:null,

    _SHOPTYPE:1,//1--卖出   2--买入

    _ITEMFORSELL:null,

    _ITEMFORBUY:null,

    _PAGE:0,

    _PAGESIZE:12,

    _MAXPAGE:0,

    _WOODNMB:100000000,

    showShop:function(parentNode,param){

        if(!param||!param.npc)
            return ;

        //console.log('交易',param);

        var uiObj = _GRM.getNodeObj('shop');

        var mainLabel = uiObj.node;

        _MSHOP._CURRENTNPCTYPE = param.npc.t;

        _MSHOP._CURRENTLABEL = mainLabel;

        _MSHOP._SHOPTYPE = 1;

        _MSHOP._PAGE = 0;

        _MSHOP.initItemArr();

        _MSHOP.refresh();

        if(uiObj.isnew){
            _MSHOP.initButton(mainLabel);
        }

        parentNode.addChild(mainLabel);

        return mainLabel;
    },

    ocShopLvUp:function(){
        var shopConf = _SHOP[_MSHOP._CURRENTNPCTYPE]
        var isopen = shopConf?(shopConf.bind!=1):false;
        var mainLabel = _MSHOP._CURRENTLABEL;
        var btnUp = mainLabel.getChildByName('mainLabel').getChildByName('btnUp');
        var btnInfo = mainLabel.getChildByName('mainLabel').getChildByName('btnInfo');
        
        if(isopen){
            btnUp.active = true;
            btnInfo.active = true;
        }else{
            btnUp.active = false;
            btnInfo.active = false;
        }
    },

    initButton:function(mainLabel){
        var tabNode = mainLabel.getChildByName('tab');
        var goodsNodes = mainLabel.getChildByName('mainLabel').getChildByName('goods').getChildren();
        var btnMain = tabNode.getChildByName('main');
        var btnUp = mainLabel.getChildByName('mainLabel').getChildByName('btnUp');
        var btnInfo = mainLabel.getChildByName('mainLabel').getChildByName('btnInfo');
        var addMain = tabNode.getChildByName('add');
        var pageNode = mainLabel.getChildByName('mainLabel').getChildByName('page');
        var btnRight = pageNode.getChildByName('btnRight');
        var btnLeft = pageNode.getChildByName('btnLeft');

        btnInfo.on(cc.Node.EventType.TOUCH_START, function(){
            var shop = _MSHOP._CURRENTNPCTYPE;
            _Ep.showMessage(_SHOPHELPER.showShopLevelDesc(shop));
        }, btnInfo);

        btnRight.on(cc.Node.EventType.TOUCH_START, function(){
            console.log('right');
            _MSHOP._PAGE++;
            _MSHOP.refresh();
        }, btnRight);

        btnUp.on(cc.Node.EventType.TOUCH_START, function(){
            //console.log("升级商店",_MSHOP._CURRENTNPCTYPE);
            if(_MSHOP.isShopLevelMax()){
                _TIPS.showMessage('该商店已满级');
            }else{
                if(_MSHOP.isXyEnough()){
                    if(_GAMEDATAHELPER.upShopLevel(_MSHOP._CURRENTNPCTYPE)){
                        _TIPS.showMessage('升级成功');
                        _MSHOP.initItemArr();
                        _MSHOP.refresh();
                    }else{
                        _TIPS.showMessage('金钱不足升级失败');
                    }
                }else{
                    _TIPS.showMessage('功利不足升级失败（功利每加200可以升一级商店,功利到达1000可以将商店升至满级）');
                }
            }
        }, btnUp);

        btnLeft.on(cc.Node.EventType.TOUCH_START, function(){
            console.log('btnLeft');
            if(_MSHOP._PAGE>0){
                _MSHOP._PAGE--;
                _MSHOP.refresh();
            }
        }, btnLeft);

        btnMain.on(cc.Node.EventType.TOUCH_START, function(){
            //console.log('销售界面');
            _MSHOP._SHOPTYPE = 1;
            _MSHOP.refresh();
        }, mainLabel);

        addMain.on(cc.Node.EventType.TOUCH_START, function(){
            //console.log('需求界面');
            _MSHOP._SHOPTYPE = 2;
            _MSHOP.refresh();
        }, mainLabel);

        for(var i=0;i<goodsNodes.length;i++){

            var goodbtn = goodsNodes[i];
            goodbtn.goodsIndex = i;
            goodbtn.on(cc.Node.EventType.TOUCH_START, function(){

                console.log(this.goodsIndex);

                var realindex = _MSHOP.getRealGoodsIndex(this.goodsIndex);

                var goodsArr = _MSHOP._SHOPTYPE==1?_MSHOP.getItemForSell():_MSHOP.getItemForBuy();

                if(!goodsArr||!goodsArr[realindex]){
                    return;
                }

                if(_MSHOP._SHOPTYPE==2){

                    _MGD.showGoodsUi(_MAP.node,
                        goodsArr[realindex],
                        {
                            sence:_MG._CURRENTSENCE,
                            pos:_MGD._POSSHOPNOSELL
                        });

                }else{

                    _MGD.showGoodsUi(_MAP.node,
                        goodsArr[realindex],
                        {
                            sence:_MG._CURRENTSENCE,
                            pos:_MGD._POSSHOP
                        });

                }

            }, goodbtn);

        }
    },

    isXyEnough:function(){
        var shop = _MSHOP._CURRENTNPCTYPE;

        var currentShopLevel = _GAMEDATAHELPER.getShopLevel(shop)+1;

        var gllevel = _GAMEDATAHELPER.getShopLevelBySelect();

        if(gllevel>=10){
            return true;
        }else{
            return currentShopLevel<(Math.floor(gllevel/2)+1);
        }
    },

    isShopLevelMax:function(){

        var shop = _MSHOP._CURRENTNPCTYPE;

        var currentShopLevel = _GAMEDATAHELPER.getShopLevel(shop)+1;

        if(_SHOP[shop]){

            var shopArr = _SHOP[shop].sell

            if(!shopArr||shopArr.length<=currentShopLevel){
                return true;
            }
        }

        return false;
    },

    getShopDesc:function(shop){

        var shopConf = _SHOP[_MSHOP._CURRENTNPCTYPE]
        var isbind = shopConf?(shopConf.bind==1):false;
        var bindtj = shopConf?(shopConf.bindtj==1):false;
        var isbindChapter = shopConf?(shopConf.bindChapter==1):false;

        if(isbind&&!isbindChapter){
            return shop+":本商店的商品会随着全局修为解锁而增加";
        }else if(isbind&&isbindChapter){
            return shop+":BOSS挑战书随全局修为解锁增加";
        }else if(bindtj){
            return shop+":解锁图鉴后开放对应的商品";
        }else{
            var currentShopLevel = _GAMEDATAHELPER.getShopLevel(shop)+1;
            if(_MSHOP.isShopLevelMax()){
                return shop
                    +"\n等级:"+currentShopLevel
                    +"\n已满级";
            }else{
                return shop
                +"\n等级:"+currentShopLevel
                +"\n升级费用:"+_CU.getUnitNmb(_MSHOP._WOODNMB);
            }
           return shop;
        }
    },

    refresh:function(){
        //var npcConfig = _NPC[_MSHOP._CURRENTNPCTYPE];
        var goodsArr = _MSHOP._SHOPTYPE==1?_MSHOP.getItemForSell():_MSHOP.getItemForBuy();
        var mainLabel = _MSHOP._CURRENTLABEL;
        var goodsNodes = mainLabel.getChildByName('mainLabel').getChildByName('goods').getChildren();
        var faceNode = mainLabel.getChildByName('mainLabel').getChildByName('face');
        var shopDescNode = faceNode.getChildByName('des');
        var faceImgNode = faceNode.getComponent(cc.Sprite);
        //var nameNode = faceNode.getChildByName('name');
        //var desNode = face.getChildByName('des');
        var pageNode = mainLabel.getChildByName('mainLabel').getChildByName('page');
        var pageNodeLabel =  pageNode.getChildByName('label');

        faceImgNode.spriteFrame = _GRM.getFaceFrame("问号");
        shopDescNode.getComponent(cc.Label).string = _MSHOP.getShopDesc(_MSHOP._CURRENTNPCTYPE);
        //nameNode.getComponent(cc.Label).string = _MSHOP._CURRENTNPCTYPE;
        if(_MSHOP._MAXPAGE>0){
            pageNode.active = true;
            pageNodeLabel.getComponent(cc.Label).string = "第"+(_MSHOP._PAGE+1)+"页";
            if(_MSHOP._PAGE>0){
                pageNode.getChildByName('btnLeft').active = true;
            }else{
                pageNode.getChildByName('btnLeft').active = false;
            }

            if(_MSHOP._PAGE<_MSHOP._MAXPAGE){
                pageNode.getChildByName('btnRight').active = true;
            }else{
                pageNode.getChildByName('btnRight').active = false;
            }
        }else{
            pageNode.active = false;
        }

        for(var i=0;i<goodsNodes.length;i++){
            var realindex = _MSHOP.getRealGoodsIndex(i);
            if(goodsArr&&goodsArr[realindex]){
                _MSHOP.refreshOneGood(goodsNodes[i],goodsArr[realindex]);
            }else{
                _MSHOP.refreshOneGood(goodsNodes[i],null);
            }

        }

        //_MSHOP.ocShopLvUp();
    },

    getRealGoodsIndex:function(i){

        return _MSHOP._PAGE*_MSHOP._PAGESIZE+i;
    },

    refreshOneGood:function(node,nodeinfo){
        var armNode = node.getChildByName('arm');
        var imgNode = armNode.getChildByName('img');
        var starNode = armNode.getChildByName('xinxin');
        var forgingNode = armNode.getChildByName('forging');

        starNode.active = false;
        forgingNode.active = false;

        if(!nodeinfo){

            imgNode.getComponent(cc.Sprite).spriteFrame = null;
            armNode.color = new cc.Color("#FFFFFF");

        }else{
            
            imgNode.getComponent(cc.Sprite).spriteFrame = _GRM.getEquipFrame(_IOF.getGoodsImg(nodeinfo));
            armNode.color = new cc.color(_IOF.getGoodsColor(nodeinfo));
        }

    },

    initItemArr:function(){
        if(
            _SHOP[_MSHOP._CURRENTNPCTYPE]
            &&_SHOP[_MSHOP._CURRENTNPCTYPE].sell
        ){
            var sellarr = _SHOP[_MSHOP._CURRENTNPCTYPE].sell;

            var currentShopLevel = _GAMEDATAHELPER.getShopLevel(_MSHOP._CURRENTNPCTYPE);

            _MSHOP._ITEMFORSELL = [];
            for(var i=0;i<=currentShopLevel;i++){
                if(sellarr&&sellarr[i]){
                    for(var j=0;j<sellarr[i].length;j++){
                        _MSHOP._ITEMFORSELL.push(_IOF.createObjByName(sellarr[i][j],null));
                    }
                }
            }
        }else if(_MSHOP._CURRENTNPCTYPE=='技能商'){
            _MSHOP._ITEMFORSELL = [];
            var sellarr = _MSHOP.getSkillByXw(false);
            for(var j=0;j<sellarr.length;j++){
                _MSHOP._ITEMFORSELL.push(_IOF.createObjByName(sellarr[j],null));
            }

        }else if(_MSHOP._CURRENTNPCTYPE=='被动技能商'){
            _MSHOP._ITEMFORSELL = [];
            var sellarr = _MSHOP.getSkillByXw(true);
            for(var j=0;j<sellarr.length;j++){
                _MSHOP._ITEMFORSELL.push(_IOF.createObjByName(sellarr[j],null));
            }

        }else{
            _MSHOP._ITEMFORSELL = null;
        }

        if(_MSHOP._ITEMFORSELL&&_MSHOP._ITEMFORSELL.length>_MSHOP._PAGESIZE){
            _MSHOP._MAXPAGE = (_MSHOP._ITEMFORSELL.length%_MSHOP._PAGESIZE==0)?
                                (_MSHOP._ITEMFORSELL.length/_MSHOP._PAGESIZE-1):
                                _MSHOP._ITEMFORSELL.length/_MSHOP._PAGESIZE;
        }else{
            _MSHOP._MAXPAGE = 0;
        }
    },

    getSkillByXw:function(isbd){

        var skillarr = [];

        if(!isbd){
            for(var key in _SKILL){
                if(_GAMEDATAHELPER.hasNpcTj(key)){
                    if(!_SKILLHEPLER.isZhuanshu(key))
                        skillarr.push(key);
                }
            }
        }else {
            for(var key in _SKILLBD){
                if(_GAMEDATAHELPER.hasNpcTj(key)){
                    if(!_SKILLHEPLER.isZhuanshu(key))
                        skillarr.push(key);
                }
            }
        }

        return skillarr;
    },

    getItemForSell:function(){
        return _MSHOP._ITEMFORSELL;
    },

    getItemForBuy:function(){
        return _MSHOP._ITEMFORBUY;
    },

    getTZS:function(xw){

        var randomJobArr = _MXLU.getObjArrOfXw('酒馆商'); 

        var enableJob = {};

        var item = [];

        for(var i=0;i<randomJobArr.length;i++){

            var jobConf = _JOB[randomJobArr[i]];

            if(jobConf.xw&&jobConf.xw[xw]){

                enableJob[randomJobArr[i]]=1;

            }

        }

        for(var key in _NPC){
            if(enableJob[_NPC[key].job]){

                if(_FIGHTITEM[key+"挑战书"]){
                    item.push(key+"挑战书");
                }

            }
        }


        return item;

    }

}

